import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

public class RedisDemoSet {
    public static void main(String[] args) {
        // 1. 连接到 Redis 服务器上.
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        // 2. 从 Redis 连接池中取一个连接出来，并及时释放
        try(Jedis jedis = jedisPool.getResource()){
//            test1(jedis);
//            test2(jedis);
//            test3(jedis);
//            test4(jedis);
//            test5(jedis);
            test6(jedis);
        }
    }

    private static void test1(Jedis jedis) {
        System.out.println("sadd n smembers");
        jedis.flushAll();

        jedis.sadd("key","111","222","333");
        Set<String> members = jedis.smembers("key");
        System.out.println(members);
    }

    private static void test2(Jedis jedis) {
        System.out.println("sismember");
        jedis.flushAll();

        jedis.sadd("key","111","222","333");
        boolean ismember = jedis.sismember("key", "111");
        System.out.println(ismember);
        ismember = jedis.sismember("key","444");
        System.out.println(ismember);

    }

    private static void test3(Jedis jedis){
        System.out.println("scard");
        jedis.flushAll();

        jedis.sadd("key","111","222","333");
        long scard = jedis.scard("key");
        System.out.println("元素个数："+scard);


    }

    private static void test4(Jedis jedis){
        System.out.println("spop");
        jedis.flushAll();

        jedis.sadd("key","111","222","333");
        String pop = jedis.spop("key");
        System.out.println(pop);
        pop = jedis.spop("key");
        System.out.println(pop);
        pop = jedis.spop("key");
        System.out.println(pop);


    }

    private static void test5(Jedis jedis){
        System.out.println("sinter");
        jedis.flushAll();

        jedis.sadd("key","111","222","333");
        jedis.sadd("key2","111","222","444");

        Set<String> result = jedis.sinter("key", "key2");
        System.out.println(result);


    }

    private static void test6(Jedis jedis){
        System.out.println("sinterstore");
        jedis.flushAll();

        jedis.sadd("key","111","222","333");
        jedis.sadd("key2","111","222","444");

        long len = jedis.sinterstore("key3", "key2","key");
        System.out.println(len);

        Set<String> members = jedis.smembers("key3");
        System.out.println(members);


    }
}
